package com.mall.controller;

import com.github.pagehelper.PageInfo;
import com.mall.common.Result;
import com.mall.model.dto.AddCategoryRequest;
import com.mall.model.dto.UpdateCategoryRequest;
import com.mall.model.po.Category;
import com.mall.model.vo.CategoryVo;
import com.mall.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @ApiOperation("产品类别增加")
    @PostMapping("/admin/category/add")
    public Result addCategory(@RequestBody @Valid AddCategoryRequest addCategoryRequest){
////       获取当前用户信息
//        User currentUser=(User) session.getAttribute(Constant.User.CURRENT_USER);
////       判断用户是否登录
//        if (currentUser==null){
//           return Result.error(MallExceptionEnum.NEED_LOGIN);
//       }
////        判断用户是否为管理员权限
//        if (!currentUser.getRole().equals(Constant.User.IS_ADMIN)){
//            return Result.error(MallExceptionEnum.NEED_ADMIN);
//        }
//        将to对象转换成po对象
//        po对象
        Category category=new Category();
//        将to对象addCategoryRequest转换成po对象category
        BeanUtils.copyProperties(addCategoryRequest,category);

        categoryService.addCategory(category);
        return Result.success();
    }
    @ApiOperation("产品类别修改")
    @PostMapping("/admin/category/update")
    public Result updateCategory(@RequestBody @Valid UpdateCategoryRequest updateCategoryRequest ){
//        将to对象转换成po对象
//        po对象
        Category category=new Category();
//        将to对象addCategoryRequest转换成po对象category
        BeanUtils.copyProperties(updateCategoryRequest,category);
        categoryService.updateCategory(category);
        return Result.success();
    }
    @PostMapping("/admin/category/delete")
    @ApiOperation("产品类别删除")
    public Result deleteCategory(@RequestParam(value = "id",required = false)@NotNull(message = "产品类别不能为null")Integer id){
        categoryService.deleteCategory(id);
        return Result.success();
    }

    @ApiOperation("产品类别列表后台")
    @GetMapping("/admin/category/list")
    public Result<PageInfo> listCategoryForAdmin(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<Category> pageInfo = categoryService.listCategoryForAdmin(pageNum,pageSize);
        return Result.success(pageInfo);
    }
    @ApiOperation("产品类别目录前台")
    @GetMapping("/category/list")
    public Result listForCustomer(){
        List<CategoryVo> list=categoryService.listCategoryForCustomer();
        return Result.success(list);
    }
}
